// pages/yuyue/index.js
var formatTime=require('../../utils/util.js')
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    workshop_id:'',
    workshop:{},
    date: '2016-09-01',
    dateForamt:'',
    date_start:'',
    date_end:'',
    name:'',
    mobile:'',
    loading:false,
    error: '',
    user:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.user && app.globalData.user != null) {
      //获取用户信息
      this.setData({
        user: app.globalData.user,
        mobile: app.globalData.user.mobile
      })
    }else{
      app.emptyUserCallback = userInfo => {
        if (user != null) {
          this.setData({
            user: app.globalData.user,
            mobile: app.globalData.user.mobile
          });
        }
    }}
      let date=new Date()
     
      let workshop_id=options.workshop_id
      this.setData({
        workshop_id:workshop_id,
        date: formatTime.formatDate(date, 'yyyy-MM-dd'),
        dateForamt: formatTime.formatDate(date, 'yyyy年MM月dd日'),
        date_start: formatTime.formatDate(date, 'yyyy-MM-dd'),
        date_end: formatTime.getCurrentMonthLast()
      })
    wx.request({
      url: app.globalData.apiurl + 'workshop/' + options.workshop_id,
      method: 'get',
      success: res => {

        if (res.data.code == 0) {
          this.setData({
            workshop: res.data.data,

          })
      
        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  //跳转投诉看厂页面
  bindtousu: function (e) {
    let url = '/pages/tousu/tousu'
    if (!this.data.mobile) {
      wx.navigateTo({
        url: '/pages/mobile/mobile?url=' + encodeURIComponent(url),
      })
      return;
    }
    wx.navigateTo({
      url: url,
    })
  },

  bindName:function(e){
    console.log(e)
    this.setData({
      name: e.detail.value
    })
  },
  bindMobile: function (e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value,
      dateForamt: formatTime.formatDate(new Date(e.detail.value), 'yyyy年MM月dd日'),
    })
  },
  
  sumitYuyue:function(e){
    let name=this.data.name
    let mobile=this.data.mobile
    let dateForamt=this.data.dateForamt
    let workshop_id=this.data.workshop_id
    if(name==''){
      this.setData({
        error:'请输入您的称呼'
      })
      return ;
    }
    if(mobile==''){
      this.setData({
        error: '请输入您的手机号'
      })
      return ;
    }
    if(this.data.loading==true){
      return;
    }
    this.setData({
      loading:true
    })
    wx.request({
      url: app.globalData.apiurl+'yuyue',
      method:'POST',
      data:{
        name,
        mobile,
        time:dateForamt,
        workshop_id:workshop_id,
        agent_id: app.globalData.userInfo.user_id
      },
      success:(res)=>{
         if(res.data.code==0){
           wx.showToast({
             title: res.data.message,
           })
           let date=new Date()
           this.setData({
             loading:false,
             name:'',
             date: formatTime.formatDate(date, 'yyyy-MM-dd'),
             dateForamt: formatTime.formatDate(date, 'yyyy年MM月dd日'),
           })
           wx.redirectTo({
             url: '/pages/detail/detail?workshop_id='+workshop_id,
           })
         }else{
           this.setData({
             error: res.data.message,
             loading: false
           })
         }
      },
      fail:(error)=>{
        console.log(error)
        this.setData({
          error:'网络错误',
          loading:false
        })
      }
    })
  }, //电话
  makeCall: function (e) {
    wx.makePhoneCall({
      phoneNumber: app.globalData.userInfo.mobile
    })
  }
})